import G from "../Utilities/Global";
import Constants from "../Constants/Constants";

/**
 * 暂停/设置界面
 * created by Five on 2018-12-24 18:33:51
 */

const { ccclass, property } = cc._decorator

@ccclass
export default class UISetting extends cc.Component {
    @property(cc.Node)
    touchPrevent: cc.Node = null;
    @property(cc.Label)
    title: cc.Label = null;
    @property(cc.Node)
    buttonMid: cc.Node = null;
    @property(cc.Sprite)
    btn_music: cc.Sprite = null;
    @property(cc.Sprite)
    btn_effect: cc.Sprite = null;

    start() {
        this.touchPrevent.on(cc.Node.EventType.TOUCH_START, (e: Event) => {
            e.stopPropagation();
        });

        let t;
        t = cc.sys.localStorage.getItem(Constants.LOC_BGMVOLUME);
        if (t != null && t != "") {
            let volume = parseInt(t);
            if (volume == 0)
                this.changeIcon(this.btn_music, "music_close");
            else
                this.changeIcon(this.btn_music, "music_open");
        }

        t = cc.sys.localStorage.getItem(Constants.LOC_SFXVOLUME);
        if (t != null && t != "") {
            let volume = parseInt(t);
            if (volume == 0)
                this.changeIcon(this.btn_effect, "effect_close");
            else
                this.changeIcon(this.btn_effect, "effect_open");
        }

        this.Close();
    }

    /**
     * 用什么方式打开
     * @param type 0 暂停 1 设置
     */
    Open(type: number) {
        if (type == 0) {
            this.title.string = "游戏暂停";
            this.buttonMid.active = true;
        } else {
            this.title.string = "系统设置";
            this.buttonMid.active = false;
        };
        this.node.active = true;
        cc.director.pause();
    }

    Close() {
        this.node.active = false;
        cc.director.resume();
    }

    Music() {
        G().Sound.playSFX("btn_clicked");

        let t = cc.sys.localStorage.getItem(Constants.LOC_BGMVOLUME);
        let volume = 0;
        if (t != null && t != "")
            volume = parseInt(t);
        if (volume == 0) {
            G().Sound.openBGM()
            this.changeIcon(this.btn_music, "music_open");
        }
        else {
            G().Sound.closeBGM();
            this.changeIcon(this.btn_music, "music_close");
        }
    }

    Effect() {
        G().Sound.playSFX("btn_clicked");
        let t = cc.sys.localStorage.getItem(Constants.LOC_SFXVOLUME);
        let volume = 0;
        if (t != null && t != "")
            volume = parseInt(t);
        if (volume == 0) {
            G().Sound.openSFX();
            this.changeIcon(this.btn_effect, "effect_open");
        }
        else {
            G().Sound.closeSFX();
            this.changeIcon(this.btn_effect, "effect_close");
        }
    }

    private async changeIcon(sprite: cc.Sprite, spName: string) {
        let spFrame = await G().Res.GetSpriteFrame("setting/" + spName) as cc.SpriteFrame;
        sprite.spriteFrame = spFrame;
    }

    Tomain() {
        G().Sound.playSFX("btn_clicked");
        G().ToMainScene();
        this.Close();
        G().Sound.playSFX("btn_clicked");
    }

    Retry() {
        G().Sound.playSFX("btn_clicked");
        G().CurScene.Restart();
        this.Close();
        G().Sound.playSFX("btn_clicked");
    }

    Continue() {
        this.Close();
        G().Sound.playSFX("btn_clicked");
    }
}